$(function(){
	$("#select-status").val($("#select-status").data("status"));
	$("#select-displaystatus").val($("#select-displaystatus").data("displaystatus"));
	$("#select-ppno-text").val($("#select-ppno-text").data("ppno"));
	$("#select-subsno-text").val($("#select-subsno-text").data("subsno"));
 });
var subscribeProductObject = {
    detail:function(obj){
    	$.post("/admin/subscribeProduct/detail/"+obj,function(data){
    		 $("#detailsModal").modal({
                 backdrop: 'static',
                 keyboard: false
             });
    		$("#detail-modal-body").html(data);
    	});
     },

     insertView:function(obj){
		$.post("/admin/subscribeProduct/insertView/",function(data){
 			$("body").html(data);
		});
	 },
	 
	 insert:function(obj){
		$("#insert-form").bootstrapValidator(validate);
		var bv = $("#insert-form").data("bootstrapValidator");
		// 手动触发验证
		bv.validate();
		if (bv.isValid()) {
			var bodyParams = subscribeProductObjectParams.bodyParams();
			var amoutParams = subscribeProductObjectParams.amoutParams();
			bodyParams.amoutParams = JSON.stringify(amoutParams);
			var url = basePath + "/admin/subscribeProduct/insert";
			var callback = function(data) {
				alert(data.Msg);
				if (data.state == "1") {
					window.location.href = basePath + "/admin/subscribeProduct/list";
				}
			}
			$.post(url, bodyParams, callback);
		}
 	 },
	 
	 update:function(obj){
		 $("#update-form").bootstrapValidator(validate);
			var bv = $("#update-form").data("bootstrapValidator");
			// 手动触发验证
			bv.validate();
			if (bv.isValid()) {
				var bodyParams = subscribeProductObjectParams.bodyUpdateParams();
				var amoutParams = subscribeProductObjectParams.amoutParams();
				bodyParams.amoutParams = JSON.stringify(amoutParams);
				var url = basePath + "/admin/subscribeProduct/update";
				var callback = function(data) {
					alert(data.Msg);
					if (data.state == "1") {
						window.location.href = basePath + "/admin/subscribeProduct/list";
					}
				}
				$.post(url, bodyParams, callback);
			}
	 },
	 updateStatus:function(obj){
		 var opid = $(obj).data("opid");
		 var status = $(obj).data("status");
		 var displaystatus = $(obj).data("displaystatus");
		 var params ={};
		 params.opid = opid;
		 if(isNotEmpty(status)){
			 params.status = status;
		 }
		 
		 if(isNotEmpty(displaystatus)){
			 params.displaystatus = displaystatus;
		 }
		 var url = basePath+"/admin/subscribeProduct/updateStatus";
		 var callback = function(data){
			 if(data.state == "1"){
				 window.location.href = basePath + "/admin/subscribeProduct/list";
			 }else{
				 alert(data.Msg);
			 }
		 }
		 $.post(url,params,callback);
	 },
	 detele:function(obj){
		 if(!confirm("您确定删除吗?"))return;
		 var url = basePath + "/admin/subscribeProduct/delete/"+obj;
		 $.post(url,function(data){
			  alert(data.Msg);
			  if(data.state == "1"){
				  window.location.href = basePath + "/admin/subscribeProduct/list";
			  }
		 });
	 },
	//显示人数限制
	isLimitNumberView:function(obj){
 		 if($(obj).val() == 1){
			 $("#limitnumber_box").show();
		 }else{
			 $("#limitnumber_box").hide();
		 }
	},
	//显示金额限制
	isLimitAmountView:function(obj){
		 if($(obj).val() == 1){
			 $("#limitamount_box").show();
		 }else{
			 $("#limitamount_box").hide();
		 }
	},
	//选择金额百分比操作
	countmodeView:function(obj){
		var objValue = $(obj).val();
 		var index = $(obj).parent().parent(".box-item").index();
 		var $BoxItem = $(".box-item").eq(index);
		if(objValue == 1){//百分比
			$BoxItem.find(".ratio").hide();
			$BoxItem.find(".quota").show();
			$BoxItem.find("input[name='quota']").val('').removeAttr("readonly");
			$BoxItem.find("input[name='minamount']").val('').removeAttr("readonly");
			$BoxItem.find("input[name='maxamount']").val('').removeAttr("readonly");
 		}else if(objValue == 2){//定额
			$BoxItem.find(".ratio").show();
			$BoxItem.find(".quota").hide();
 			$BoxItem.find("input[name='ratio']").val('').removeAttr("readonly");
 			$BoxItem.find("input[name='minamount']").val('--').attr("readonly","");
			$BoxItem.find("input[name='maxamount']").val('--').attr("readonly","");
		}else{
			$BoxItem.find(".ratio").show();
			$BoxItem.find(".quota").hide();
			$BoxItem.find("input[name='ratio']").val('--').attr("readonly","");
			$BoxItem.find("input[name='quota']").val('--').attr("readonly","");
			$BoxItem.find("input[name='minamount']").val('--').attr("readonly","");
			$BoxItem.find("input[name='maxamount']").val('--').attr("readonly","");
 		}
	},
	//根据产品编号显示产品信息
	showDetailByPpNO:function(obj){
		var ppno = $(obj).val();
		if(isEmpty(ppno)){return;}
		var callback = function(data){
			$("#ppnoMsg").css("color","");
			$("#ProductDetail_box").html("");
			if(data.state == 1){
 				$("#ppnoMsg").text(data.objectlist.pname +"-"+ data.objectlist.ppname);
 				var htmlConu = "";
   				$.each(data.obj2,function(i,c){
   					htmlConu += "<div class='col-md-12 box-item' style='margin-top:15px;'>"+
"							<div class='col-md-2 text-center'>"+
"								<span class='gear'>"+c.gear+"</span>"+
"							</div>"+
"							<div class='col-md-2 text-center'>"+
"								<select class='form-control' name='countmode' onchange='subscribeProductObject.countmodeView(this)'>"+
"									<option value=''>--请选择--</option>"+
"									<option value='1'>百分比</option>"+
"									<option value='2'>定额</option>"+
"								</select>"+
"							</div>"+
"							<div class='col-md-2 text-center clearfix ratio'>"+
"								<input type='text' onkeyup='clearNoNum(this);' value='--' name='ratio' class='form-control col-md-1 pull-left text-center' readonly style='width:85%;'>"+
"								<label class='pull-right' style='width:10px;line-height:30px;'>元</label>"+
"							</div>"+
"							<div class='col-md-2 text-center clearfix quota'style='display:none;'>"+
"								<input type='text' onkeyup='clearNoNum(this);' value='--' name='quota' class='form-control col-md-1 pull-left text-center' readonly style='width:85%;'>"+
"								<label class='pull-right' style='width:10px;line-height:30px;'>%</label>"+
"							</div>"+
"							<div class='col-md-2 text-center'>"+
"								<input type='text' onkeyup='clearNoNum(this);' value='--' name='minamount' class='form-control text-center' readonly>"+
"							</div>"+
"							<div class='col-md-2 text-center'>"+
"								<input type='text' onkeyup='clearNoNum(this);' value='--' name='maxamount' class='form-control text-center' readonly>"+
"							</div>"+
"						</div>";
 				});
 				$("#ProductDetail_box").html(htmlConu);
			}else{
				$("#ppnoMsg").css("color","red").text(data.Msg);
 			}
		}
 		$.post(basePath+"/admin/subscribeProduct/showDetailByPpNO",{"ppno":ppno},callback);
	}
}

//验证
var validate = {
		icon : {
			valid : 'glyphicon glyphicon-ok',
			invalid : 'glyphicon glyphicon-remove',
			validating : 'glyphicon glyphicon-refresh'
		},
		fields : {
			ppno : {
				validators : {
					notEmpty : {
						message : "* 请输入产品编号"
					} 
				}
			},
			endtime : {
				validators : {
					callback:{
						message:"* 请选择结束时间",
						callback:function(value,validator){
  							if(isEmpty(value)){
 								 return false;
							}else{
 								 return true;
							}
						}
					}
				}
			},
			limitnumber : {
				validators : {
					notEmpty : {
						message : "* 请填写限制人数"
					} 
				}
			},
			limitamount : {
				validators : {
					notEmpty : {
						message : "* 请填写金额限制"
					} 
				}
			},
			islimitnumber : {
				validators : {
 					callback:{
						message:"* 请选择人数限制",
						callback:function(value,validator){
							if(isEmpty(value)){
 								 return false;
							}else{
 								 return true;
							}
						}
					}
				}
			},
			islimitamount : {
				validators : {
 					callback:{
						message:"* 请选择限制金额",
						callback:function(value,validator){
							if(isEmpty(value)){
 								 return false;
							}else{
 								 return true;
							}
						}
					}
				}
			},
			displaystatus : {
				validators : {
 					callback:{
						message:"* 请选择显示状态",
						callback:function(value,validator){
							if(isEmpty(value)){
 								 return false;
							}else{
 								 return true;
							}
						}
					}
				}
			}
//			,
//			limitnumber : {
//				validators : {
// 					callback:{
//						message:"* 请选择人数限制",
//						callback:function(value,validator){
//							if(isEmpty(value)){
// 								 return false;
//							}else{
// 								 return true;
//							}
//						}
//					}
//				}
//			}
		} 
}


var subscribeProductObjectParams = {
		bodyParams:function(){
			var $fromObj = $("#insert-form");
			var ppno = $fromObj.find("input[name='ppno']").val();
			var islimitnumber = $fromObj.find("select[name='islimitnumber']").val();
			var limitnumber = $fromObj.find("input[name='limitnumber']").val();
			var islimitamount = $fromObj.find("select[name='islimitamount']").val();
			var limitamount = $fromObj.find("input[name='limitamount']").val();
			var status = $fromObj.find("select[name='status']").val();
			var displaystatus = $fromObj.find("select[name='displaystatus']").val();
			var sortnum = $fromObj.find("input[name='sortnum']").val();
			var endtime = $fromObj.find("input[name='endtime']").val();
			var description = $fromObj.find("textarea[name='description']").val();
			var remark = $fromObj.find("textarea[name='remark']").val();
  			var params = {};
  			params.ppno = ppno;
  			params.islimitnumber = islimitnumber;
  			if(islimitnumber == 1){
   				params.limitnumber = limitnumber;
  			}
  			params.islimitamount = islimitamount;
  			if(islimitamount == 1){
   				params.limitamount = limitamount;
  			}
  			params.status = status;
  			params.displaystatus = displaystatus;
  			params.sortnum = sortnum;
  			params.endtime = endtime;
  			params.description = description;
  			params.remark = remark;
 			return params;
		},
		bodyUpdateParams:function(){
			var $fromObj = $("#update-form");
			var ppno = $fromObj.find("input[name='ppno']").val();
 			var islimitnumber = $fromObj.find("select[name='islimitnumber']").val();
			var limitnumber = $fromObj.find("input[name='limitnumber']").val();
			var id = $fromObj.find("input[name='id']").val();
			var islimitamount = $fromObj.find("select[name='islimitamount']").val();
			var limitamount = $fromObj.find("input[name='limitamount']").val();
 			var displaystatus = $fromObj.find("select[name='displaystatus']").val();
			var sortnum = $fromObj.find("input[name='sortnum']").val();
			var endtime = $fromObj.find("input[name='endtime']").val();
 			var description = $fromObj.find("textarea[name='description']").val();
			var remark = $fromObj.find("textarea[name='remark']").val();
  			var params = {};
  			params.ppno = ppno;
   			params.islimitnumber = islimitnumber;
  			if(islimitnumber == 1){
   				params.limitnumber = limitnumber;
  			}
  			params.islimitamount = islimitamount;
  			if(islimitamount == 1){
   				params.limitamount = limitamount;
  			}
   			params.displaystatus = displaystatus;
  			params.sortnum = sortnum;
   			params.description = description;
  			params.remark = remark;
  			params.endtime = endtime;
  			params.id = id;
 			return params;
		},
		amoutParams:function(){
			var length = $(".box-item").length;
			var $BoxItem = $(".box-item");
 			var amoutParams = new Array();
			for(var i =0;i < length;i++){
				var Obj = {};
				var gear = $BoxItem.eq(i).find(".gear").text();
				var id = $BoxItem.eq(i).find("input[name='id']").val();
				if(isNotEmpty(id)){
					Obj.id= id;
				}
				var countmode = $BoxItem.eq(i).find("select[name='countmode']").val();
				var ratio = $BoxItem.eq(i).find("input[name='ratio']").val();
				var minamount = $BoxItem.eq(i).find("input[name='minamount']").val();
				var maxamount = $BoxItem.eq(i).find("input[name='maxamount']").val();
				var quota = $BoxItem.eq(i).find("input[name='quota']").val();
				Obj.gear = gear;
				Obj.countmode = countmode;
				if(countmode == 1){//百分比
					Obj.quota = quota;
					Obj.minamount = minamount;
					Obj.maxamount = maxamount;
 				}else if(countmode == 2){//定额 
					Obj.ratio = ratio;
 				}
				amoutParams.push(Obj);
 			}
 			return amoutParams;
		}
}

function queryAllPerson(pageNum,pageSize){
 	 $("#pageNum").val(pageNum);
 	 $("#pageSize").val(pageSize);
	 $("#form-select").submit();
}

function queryAllPersonView(pageNum,pageSize){
	 $("#pageNumView").val(pageNum);
	 $("#pageSizeView").val(pageSize);
	 $("#form-selectView").submit();
}

function clearNoNum(obj){  
    //修复第一个字符是小数点 的情况.  
    if(obj.value !=''&& obj.value.substr(0,1) == '.'){  
        obj.value="";  
    }  
    obj.value = obj.value.replace(/^0*(0\.|[1-9])/, '$1');//解决 粘贴不生效  
    obj.value = obj.value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符  
    obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的       
    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");      
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数       
    if(obj.value.indexOf(".")< 0 && obj.value !=""){//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额  
        if(obj.value.substr(0,1) == '0' && obj.value.length == 2){  
            obj.value= obj.value.substr(1,obj.value.length);      
        }  
    }      
} 

